AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')
//Npcs that aren't supposed to have flesh
ENT.MetalicNPCs =
{
	"npc_manhack",
	"npc_cscanner",
	"npc_dog",
	"npc_combinedropship",
	"npc_combinegunship",
	"npc_turret_ceiling",
	"npc_turret_floor",
	"npc_rollermine",
	"npc_helicopter",
	"npc_strider"
}

function ENT:Initialize()
	self.Entity:SetModel("models/misc/setubun/soybean.mdl");
	self.Entity:PhysicsInit(SOLID_VPHYSICS);
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS);
	self.Entity:SetSolid(SOLID_VPHYSICS);
	self.Entity:SetCollisionGroup(COLLISION_GROUP_PROJECTILE)
	local phys = self:GetPhysicsObject()
	if phys:IsValid() then
		phys:Wake()
	end
	//variables that are best placed in initalize
	self.AlreadyHit = false;
	self.AlreadyPlayed = false;
end

function ENT:Think()
end

function ENT:SpawnFunction(ply, tr)

	if (!tr.Hit) then return end

	local SpawnPos = tr.HitPos + tr.HitNormal * 16;

	local ent = ents.Create("smod_soybean");
	ent:SetPos(SpawnPos);
	ent:Spawn();
	ent:Activate();
	return ent;
end

function ENT:StartTouch(ent)
	if (!self.AlreadyHit) then -- have we hit something before?
		self.Entity:EmitSound(Sound("weapons/mamehand/soy.wav"))
		ent:TakeDamage(1.5,self.Entity:GetOwner(),self.Entity) -- damage anything
		self.AlreadyHit = true -- now we have hit something
		timer.Simple(6,function() if ValidEntity(self.Entity) then self.Entity:Remove() end end) -- remove beans after a while
	end
end

function ENT:PhysicsCollide(Data)
	local ent = Data.HitEntity;
	if ent:IsPlayer() || (ent:IsNPC() && !table.HasValue(self.MetalicNPCs,ent:GetClass())) then --check if it's a player or npc that isn't metallic
		//blood effect
		local FX = EffectData();
		FX:SetOrigin(Data.HitPos);
		util.Effect("BloodImpact",FX,true,true);
	end
	
		if (Data.HitEntity:IsWorld()) then	
			timer.Simple(6,function() if ValidEntity(self.Entity) then self.Entity:Remove() end end) --incase the first timer didn't work, remove beans after a while
			if (!self.AlreadyPlayed) then -- have we played the sound?
			self.Entity:EmitSound(Sound("weapons/mamehand/soy.wav"))
			self.AlreadyPlayed = true -- now we have
			end
				if (Data.HitEntity:IsWorld() && self.Entity:IsOnGround()) then
					self.Entity:SetCollisionGroup(COLLISION_GROUP_NONE) --don't collide
					self.Entity:SetSolid(SOLID_NONE) -- don't act with anything, not physgunpickupable
				end
		end
end 